using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class QuestSystem
{
    static private QuestSystem Instance = null;

    static public QuestSystem GetInstance()
    {
        if (Instance == null)
        {
            Instance = new QuestSystem();
        }
        return Instance;
    }
    List<Quest> Quests = new List<Quest>();
    List<Quest> QuestFinished = new List<Quest>();
    List<Quest> QuestActive = new List<Quest>();

    public void Active(string id)
    {
        for (int i = 0; i < Quests.Count; i++)
        {
            if (Quests[i].Id == id)
            {
                //si no esta activa la agrego.
                if (!Quests[i].Active)
                {
                    Quests[i].Active = true;
                    QuestActive.Add(Quests[i]);
                    //Debug.Log("Quest: " + id + " activada");
                }
            }
        }
    }
    public void Active(Quest quest)
    {
        //si no esta activa la agrego.
        if (!quest.Active)
        {
            quest.Active = true;
            QuestActive.Add(quest);
            //Debug.Log("Quest: " + id + " activada");
        }
    }
    public void SendInfo(string id, int value, string info)
    {
        //Debug.Log(QuestActive.Count);
        for (int i = 0; i < QuestActive.Count; i++)
        {
            QuestActive[i].SendInfo(id, value, info);
        }
        Check();
    }
    /// <summary>
    /// Se fija si alguna quest termino sus requerimientos.
    /// </summary>
    private void Check()
    {
        for (int i = 0; i < QuestActive.Count; i++)
        {
            if (QuestActive[i].Check())
            {
                CompleteQuest(QuestActive[i]);
            }
        }
    }
    /// <summary>
    /// Completa una quest y activa las que tenga de hijos.
    /// </summary>
    /// <param name="quest"></param>
    public void CompleteQuest(Quest quest)
    {
        QuestActive.Remove(quest);
        quest.Active = false;
        QuestFinished.Add(quest);
        for (int i = 0; i < quest.ChildsId.Count; i++)
        {
            Active(quest.ChildsId[i]);
        }
        if (quest.ChildsId.Count == 0)
        {
            GameManager.GetInstance().WinGame();
        }
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="id"></param>
    /// <param name="text"></param>
    /// <returns>Devuelve la quest para dps agregarle objetivos</returns>
    public Quest Add(string id, string text)
    {
        Quest q = new Quest();
        q.Id = id;
        q.Text = text;

        Quests.Add(q);

        return q;
    }
    public QuestState GetQuestState(string id)
    {
        for (int i = 0; i < QuestFinished.Count; i++)
        {
            if (QuestFinished[i].Id == id)
            {
                return QuestState.COMPLETED;
            }
        }
        for (int i = 0; i < QuestActive.Count; i++)
        {
            if (QuestActive[i].Id == id)
            {
                return QuestState.ACTIVE;
            }
        }
        return QuestState.UNACTIVE;
    }
    public void ShowQuests()
    {
        int k = 0;
        for (int i = 0; i < QuestActive.Count; i++)
        {
            for (int j = 0; j < QuestActive[i].Objectives.Count; j++)
            {
                Objective e = QuestActive[i].Objectives[j];
                GUI.Label(new Rect(Screen.width - 200, 25 * k, 200, 100), e.Text + "  " + e.CurrentAmount + "/" + e.TotalAmount);
                k++;

            }
        }
    }
    /// <summary>
    /// Setea la instancia estatica en null.
    /// </summary>
    public void ResetInstance()
    {
        Instance = null;
    }

}
public enum QuestState
{
    UNACTIVE,
    ACTIVE,
    COMPLETED,
}
